<template>
	<view class="currentPage" @touchstart='touchstart' @touchmove='touchmove' @click="pageClick">
		<navbar title='消息列表' :avatar="userInfo && userInfo.avatar">
			<view class="addFriend" slot='opBtn'>
				<image src="/static/icon/navbar/add_friend.png" mode="" @tap='openMenu'></image>
				<popup-menu @popupMenuClickItem='popupMenuClickItem' v-model="popupMenuShow"></popup-menu>
			</view>
		</navbar>
		<u-alert-tips :type="type" :title="title" :show-icon="true"></u-alert-tips>
		<!-- <view class="item">
			<view></view>
			<view></view>
			<view></view>
		</view> -->
		<view class="msg-list">
			<view class="list-item" v-for="(item,key) in chatList" @click="toChat(item,key)" :key='key'>
				<!-- {{item}} -->
				<view class="left">
					<view class="avatar">
						<image :src="item.avatar" mode=""></image>
					</view>
					<view class="user-msg">
						<view class="user">{{item.from.sender}}</view>
						<view class="msg" v-if="item.type === 'text'">{{item.content}}</view>
						<view class="msg" v-if="item.type === 'image'">[图片]</view>
						<view class="msg" v-if="item.type === 'voice'">[语音]</view>
					</view>
				</view>
				<view class="right">
					<view class="time">{{new Date(item.date).getHours() + ':' + new Date(item.date).getMinutes()}}
					</view>
					<view class="msg-num" v-show="item.nums > 0">{{item.nums}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import search from "@/components/search.vue"
	import mySelf from "@/components/my_self.vue"
	import tabIndex from "../tabbarPage/index.vue"
	import tabContacts from "../tabbarPage/contacts.vue"
	import tabCenter from "../tabbarPage/center.vue"
	import navBar from '@/components/navbar.vue'
	import popupMenu from "@/components/popupMenu.vue"
	import {
		mapState
	} from "vuex"
	
	export default {
		components: {
			search,
			mySelf,
			tabIndex,
			tabContacts,
			tabCenter,
			navBar,
			popupMenu
		},

		data() {
			return {
				title: '未连接到聊天服务器',
				description: '未链接到服务器',
				type: 'error',
				index: 0,
				show: false,
				current: 1,
				disableTouch: true,
				popupMenuShow: false,
				chatList: {}
			}
		},
		computed: {
			...mapState(['userInfo', 'phoneInfo'])
		},
		onNavigationBarButtonTap(e) {
			console.log(e)
			//侧滑打开页面
			if (e.key == 'self') {
				this.$refs.my_self.open()
			} else {
				this.show = true
			}

		},
		onShow() {
			console.log('onSHow', this.$socket)
			let timerId = setInterval(() => {
				if (this.$socket) {
					console.log('lookMe', this.$socket)
					clearInterval(timerId)
					this.type = 'success'
					this.title = '聊天服务器已连接'
				} else {
					console.log('连接socket', this.userInfo)
					if (this.userInfo) {
						this.$initSocket(this.userInfo.id)
					}
				}
			}, 500)
		},
		onLoad(e) {
			//打开数据库
			//判断数据库是否打开
			// #ifdef APP-PLUS
			// this.$isOpenDatabase('chat', 'chatDB').then(openRes => {
			// 	console.log(openRes)
			// 	//未打开，打开数据库
			// 	if (!openRes) {
			// 		console.log(`数据库chat未打开，准备打开`)
			// 		this.$openDatabase('chat', 'chatDB').then(res => {
			// 			console.log(res)
			// 			if (res.code === 1) {
			// 				console.log('数据库chat已打开')
			// 			}
			// 		})
			// 	} else {
			// 		console.log('数据库chat已打开')
			// 	}
			// })
			this.$opChatDB.queryAllTable('chat').then(res => {
				console.log('首页查询数据库所有表', res)
			})
			// #endif
		},
		mounted() {
			uni.$on('avatarTap', () => {
				this.current = 0
			})
			uni.$on('privateMsg_SystemToUser', data => {
				console.log('index页面接收消息', data)
				data.isSelf = 'n'
				if (this.chatList[data.from.senderId]) {
					console.log('存在数量',this.chatList[data.from.senderId].nums)
					let nums = this.chatList[data.from.senderId].nums += 1
					data.nums = nums
					this.chatList[data.from.senderId] = data
				} else {
					console.log('不存在数量')
					this.chatList[data.from.senderId] = data
					this.chatList[data.from.senderId]['nums'] = 0
					this.chatList[data.from.senderId]['nums'] += 1
				}

				this.$forceUpdate()
				console.log(this.chatList)
			})

		},
		onUnload() {},
		methods: {
			toChat(item, key) {
				// console.log(item,index)
				// return
				this.chatList[key].nums = 0
				uni.navigateTo({
					url: `/pages/privateChatRoom/privateChatRoom?receiverId=${key}`
				})
			},
			pageClick(e) {
				// console.log('index页面被点击',e)
				// this.popupMenuShow = false
			},
			popupMenuClickItem(e) {
				console.log(e)
				this.popupMenuShow = false
				if (e.label === '添加好友') {
					uni.navigateTo({
						url: '/pages/addFriend/addFriend'
					})
				}
				if (e.label === '扫一扫') {
					uni.scanCode({
						success: (res) => {
							console.log(res)
							// {
							// 	"scanType": "CODE_128",
							// 	"path": "",
							// 	"charSet": "utf8",
							// 	"result": "30847/20Y300519",
							// 	"errMsg": "scanCode:ok"
							// }

						}
					})
				}
			},
			openMenu() {
				console.log('打开菜单')
				this.popupMenuShow = true
			},
			//改变页面显示
			changeTabIndex(e) {
				this.index = e
				if (e == 0) {
					this.$refs.index.reloadData()
				}
				if (e == 1) {
					this.$refs.contacts.reloadData()
				}
			},
			swiperChange(e) {
				this.current = e.detail.current
			},
			changCurrent() {
				this.current = 0
			},
			touchstart(e) {
				this.touchS = e.changedTouches[0]
			},
			touchmove(e) {
				//计算y轴浮动差
				let diffY = e.changedTouches[0].clientY - this.touchS.clientY
				let diffX = e.changedTouches[0].clientX - this.touchS.clientX
				if (Math.abs(diffY) < 25 && Math.abs(diffX) > 40) {
					//判断滑动方向
					//1 结束时x 大于 开始是x 向右
					if (Math.abs(e.changedTouches[0].clientX) > Math.abs(this.touchS.clientX)) {
						this.current = 0
						console.log('右')
					}
					if (Math.abs(e.changedTouches[0].clientX) < Math.abs(this.touchS.clientX)) {
						this.current = 1
						console.log('左')
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.currentPage {
		min-height: 100vh;
		padding-bottom: 50rpx;
		background-color: rgb(245, 245, 245);
	}

	.addFriend {
		position: absolute;
		right: 15rpx;
		bottom: 10rpx;

		/*  */
		image {
			width: 50rpx;
			height: 50rpx;
		}

	}

	.msg-list {
		.list-item {
			// height: 100rpx;
			// border: 1px solid;
			padding: 20rpx 0;
			margin: 0 30rpx;
			display: flex;
			justify-content: space-between;
			// background-color: orange;

			&:not(:last-child) {
				border-bottom: 1px solid rgba($color: gray, $alpha: .2);
			}

			.left {
				display: flex;

				.avatar {
					image {
						height: 100rpx;
						width: 100rpx;
						border-radius: 10rpx;
					}
				}

				.user-msg {
					margin-left: 30rpx;

					.user {
						font-size: 36rpx;
					}

					.msg {
						margin: 10rpx 0;
						color: rgba($color: gray, $alpha: .5);
					}
				}
			}

			.right {
				text-align: center;

				.time {}

				.msg-num {
					margin: 10rpx auto;
					// border: 1px solid;
					font-size: 24rpx;
					width: 46rpx;
					height: 46rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 50%;
					background-color: red;
					color: #FFFFFF;
				}
			}

		}
	}

	.item {
		display: flex;
		justify-content: space-between;
		margin: 30rpx;

		view {
			height: 200rpx;
			width: 200rpx;
			box-shadow: 0 0 10rpx rgba($color: gray, $alpha: .3);
			border-radius: 10rpx;

		}
	}
</style>
